package com.rp.system.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rp.common.utils.RpUtil;
import com.rp.log.annotation.RpLog;
import com.rp.security.extend.MySecurityMetadataSource;
import com.rp.system.dao.AuthorityDao;
import com.rp.system.po.Authority;
import com.rp.system.po.Role;

@Service
@Transactional
public class AuthorityService {

	@Autowired
	private AuthorityDao authorityDao;
	
	@Autowired
	private MySecurityMetadataSource mySecurityMetadataSource;
	
	@RpLog(desc="保存权限")
	public String saveAuthority(Authority auth){
		auth.setCreateDate(RpUtil.getCurrentDatetime());
		return (String) authorityDao.save(auth);
	}
	
	@RpLog(desc="更新权限")
	public void updateAuthority(Authority auth) throws Exception{
		auth.setUpdateDate(RpUtil.getCurrentDatetime());
		authorityDao.update(auth);
		mySecurityMetadataSource.init();
	}
	
	/**
	 * 删除权限
	 * @param authorityId
	 */
	@RpLog(desc="删除权限")
	public void deleteAuthority(String authorityId){
		Authority auth = authorityDao.get(authorityId);
		List<Role> roles = auth.getRoles();
		for (Role role : roles) {
			role.getAuthorities().remove(auth);
		}
		auth.setResources(null);
		authorityDao.delete(auth);
	}
	
	
	
	
	
	public AuthorityDao getAuthorityDao() {
		return authorityDao;
	}
	
}
